//
//  MyOrderViewController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/25.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "MyOrderViewController.h"
#import "OrderListTableCell.h"
#import "OrderListViewModel.h"
#import "ResponseOrderListModel.h"
#import "OrderDetailViewController.h"
@interface MyOrderViewController ()

@property (weak, nonatomic) IBOutlet UITableView *mainTable;
@property (strong,nonatomic) OrderListViewModel *orderListViewModel;

@end

@implementation MyOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setLeftBtnItem];
    self.mainTable.alpha = 0;
    self.orderListViewModel = [[OrderListViewModel alloc ] init];
    [self loadNewData];
    
    // 头部刷新
    WEAKSELF
    [self addHeader:^() {
        [weakSelf loadNewData];
    } tableView:self.mainTable];
    
    
    self.mainTable.mj_footer = [MJDIYBackFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}


-(void)loadNewData{
    self.orderListViewModel.pageIndex = 1;
    [self loadData];
}

-(void)loadMoreData{
    self.orderListViewModel.pageIndex ++;
    [self loadData];
}


-(void)loadData{
    WEAKSELF
    [self.orderListViewModel loadOrderListData:^(NSInteger resultType, id data, NSString *errorMsg) {
        
        [weakSelf.mainTable.mj_header endRefreshing];
        [weakSelf.mainTable.mj_footer endRefreshing];
        if (resultType == 1|| resultType == 3) {
            
            weakSelf.mainTable.alpha = 1;
            [weakSelf.mainTable reloadData];
            
            if (resultType == 3) {
                weakSelf.mainTable.mj_footer.hidden = YES;
            }else{
                weakSelf.mainTable.mj_footer.hidden = NO;
        
            }
        }else{
            [SVProgressHUD showErrorWithStatus:errorMsg];
        }
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   return 170;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return self.orderListViewModel.orderList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderListTableCell *orderListCell = [tableView dequeueReusableCellWithIdentifier:@"OrderListTableCell"];
    [orderListCell setModel:[self.orderListViewModel.orderList objectAtIndex:indexPath.row]];
    return orderListCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ResponseOrderListModel *entity = [self.orderListViewModel.orderList objectAtIndex:indexPath.row];
    OrderDetailViewController *orderDetailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OrderDetailViewController"];
    orderDetailViewController.orderDetailViewModel.TicketOrderId = entity.TicketOrderId;
    
    if (entity.TicketType == 1) {
        orderDetailViewController.isRegularBus = YES;
    }
    
    [self.navigationController pushViewController:orderDetailViewController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
